package edu.zut.main.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils02 {
    public static void main(String[] args) {

    }
    static Connection connection = null;
    static PreparedStatement statement = null;
    static ResultSet resultSet = null;
    private static DataSource dataSource = null;

    static{
        dataSource = new ComboPooledDataSource("mysql");
    }
    /**
     * 获取数据库连接
     * @return Connection
     */
    public static Connection getConnection(){
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    //关闭数据库连接资源
    public static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        try {
            if (resultSet != null)
                resultSet.close();
            if (statement != null)
                statement.close();
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
